import sys

from PyQt5.QtWidgets import QMainWindow,QApplication, QVBoxLayout, QHBoxLayout, QLineEdit,QPushButton,QWidget,QLabel,QFileDialog,QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
from Coordinate_Transformation_Lib.tramsfom import WGS84toGCJ02

class MainWindow(QMainWindow):
    def __init__(self) -> None:
        super().__init__()
        self.output_file_path = ''
        self.input_file_path = ''
        self.setupUI()
    
    def setupUI(self):
        self.resize(400,200)
        self.setWindowTitle("WGS84转GCJ02工具")
        self.setFixedWidth(400)

        # 输入
        input_layout_h = QHBoxLayout()
        input_label = QLabel("输入：")
        self.input_line_edit = QLineEdit()
        input_button = QPushButton("...")

        input_layout_h.addWidget(input_label)
        input_layout_h.addWidget(self.input_line_edit)
        input_layout_h.addWidget(input_button)

        input_button.clicked.connect(self.open_input_file)
        

        # 输出
        output_layout_h = QHBoxLayout()
        output_label = QLabel("输出：")
        self.output_line_edit = QLineEdit()
        output_button = QPushButton("...")

        output_layout_h.addWidget(output_label)
        output_layout_h.addWidget(self.output_line_edit)
        output_layout_h.addWidget(output_button)

        output_button.clicked.connect(self.open_output_file)

        #按钮
        button_layout_h = QHBoxLayout()
        submit_button = QPushButton("点击执行坐标转换")
        submit_button.setFixedWidth(150)
        button_layout_h.addWidget(submit_button)
        submit_button.clicked.connect(self.transformcoordinates)

        widget = QWidget()
        vbox = QVBoxLayout()
        vbox.addLayout(input_layout_h)
        vbox.addLayout(output_layout_h)
        vbox.addLayout(button_layout_h)
        widget.setLayout(vbox)

        self.setCentralWidget(widget)
    
    def open_input_file(self):
        file_path = QFileDialog.getOpenFileName(filter="文本文档(*.txt);;分隔符文件(*.csv)")
        self.input_file_path = file_path[0]
        self.input_line_edit.setText(self.input_file_path)
    
    def open_output_file(self):
        file_path = QFileDialog.getSaveFileName(filter="文本文档(*.txt);;分隔符文件(*.csv)")
        self.output_file_path = file_path[0]
        self.output_line_edit.setText(self.output_file_path)

    def transformcoordinates(self):
        if self.input_file_path == '' or self.output_file_path == '':
            QMessageBox.warning(self, '警告', '请补全输入输出路径！')
        else:
            content = ''
            with open(self.input_file_path,"r") as inputfile:
                trans = WGS84toGCJ02()
                for line in inputfile.readlines():
                    line_list = line.split(",")
                    name = line_list[0],
                    longitude = line_list[1]
                    latitude = line_list[2]
                    height = line_list[3]

                    result = trans.wgs84_gcj02([float(longitude),float(latitude)])
                    f_longitude = str(result[0])
                    f_latitude = str(result[1])
                    content = content + name[0] + "," + f_longitude + "," + f_latitude + "," + height

            inputfile.close()

            with open(self.output_file_path, "w") as outputfile:
                outputfile.write(content)

            outputfile.close()
            QMessageBox.about(self, u'提示', u"\n坐标文件转换完成！")
        

if __name__ == '__main__': 
     app = QApplication(sys.argv)
     window = MainWindow()
     window.show()
     sys.exit(app.exec_())
